package cm.mcmod.jm.procedures;

import net.minecraft.item.ItemStack;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.Entity;
import net.minecraft.enchantment.EnchantmentHelper;

import java.util.Random;
import java.util.Map;

import cm.mcmod.jm.JmMod;

public class UnremindineDangShiWuBeiChiShiProcedure {

	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			if (!dependencies.containsKey("entity"))
				JmMod.LOGGER.warn("Failed to load dependency entity for procedure UnremindineDangShiWuBeiChiShi!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		if (entity instanceof LivingEntity) {
			if (entity instanceof PlayerEntity)
				((PlayerEntity) entity).inventory.armorInventory.set((int) 0, (EnchantmentHelper.addRandomEnchantment(new Random(),
						((entity instanceof LivingEntity) ? ((LivingEntity) entity).getItemStackFromSlot(EquipmentSlotType.FEET) : ItemStack.EMPTY),
						(int) ((entity instanceof PlayerEntity) ? ((PlayerEntity) entity).experienceLevel : 0), (false))));
			else
				((LivingEntity) entity).setItemStackToSlot(EquipmentSlotType.FEET, (EnchantmentHelper.addRandomEnchantment(new Random(),
						((entity instanceof LivingEntity) ? ((LivingEntity) entity).getItemStackFromSlot(EquipmentSlotType.FEET) : ItemStack.EMPTY),
						(int) ((entity instanceof PlayerEntity) ? ((PlayerEntity) entity).experienceLevel : 0), (false))));
			if (entity instanceof ServerPlayerEntity)
				((ServerPlayerEntity) entity).inventory.markDirty();
		}
		if (entity instanceof LivingEntity) {
			if (entity instanceof PlayerEntity)
				((PlayerEntity) entity).inventory.armorInventory.set((int) 1, (EnchantmentHelper.addRandomEnchantment(new Random(),
						((entity instanceof LivingEntity) ? ((LivingEntity) entity).getItemStackFromSlot(EquipmentSlotType.LEGS) : ItemStack.EMPTY),
						(int) ((entity instanceof PlayerEntity) ? ((PlayerEntity) entity).experienceLevel : 0), (false))));
			else
				((LivingEntity) entity).setItemStackToSlot(EquipmentSlotType.LEGS, (EnchantmentHelper.addRandomEnchantment(new Random(),
						((entity instanceof LivingEntity) ? ((LivingEntity) entity).getItemStackFromSlot(EquipmentSlotType.LEGS) : ItemStack.EMPTY),
						(int) ((entity instanceof PlayerEntity) ? ((PlayerEntity) entity).experienceLevel : 0), (false))));
			if (entity instanceof ServerPlayerEntity)
				((ServerPlayerEntity) entity).inventory.markDirty();
		}
		if (entity instanceof LivingEntity) {
			if (entity instanceof PlayerEntity)
				((PlayerEntity) entity).inventory.armorInventory.set((int) 2, (EnchantmentHelper.addRandomEnchantment(new Random(),
						((entity instanceof LivingEntity) ? ((LivingEntity) entity).getItemStackFromSlot(EquipmentSlotType.CHEST) : ItemStack.EMPTY),
						(int) ((entity instanceof PlayerEntity) ? ((PlayerEntity) entity).experienceLevel : 0), (false))));
			else
				((LivingEntity) entity).setItemStackToSlot(EquipmentSlotType.CHEST, (EnchantmentHelper.addRandomEnchantment(new Random(),
						((entity instanceof LivingEntity) ? ((LivingEntity) entity).getItemStackFromSlot(EquipmentSlotType.CHEST) : ItemStack.EMPTY),
						(int) ((entity instanceof PlayerEntity) ? ((PlayerEntity) entity).experienceLevel : 0), (false))));
			if (entity instanceof ServerPlayerEntity)
				((ServerPlayerEntity) entity).inventory.markDirty();
		}
		if (entity instanceof LivingEntity) {
			if (entity instanceof PlayerEntity)
				((PlayerEntity) entity).inventory.armorInventory.set((int) 3, (EnchantmentHelper.addRandomEnchantment(new Random(),
						((entity instanceof LivingEntity) ? ((LivingEntity) entity).getItemStackFromSlot(EquipmentSlotType.HEAD) : ItemStack.EMPTY),
						(int) ((entity instanceof PlayerEntity) ? ((PlayerEntity) entity).experienceLevel : 0), (false))));
			else
				((LivingEntity) entity).setItemStackToSlot(EquipmentSlotType.HEAD, (EnchantmentHelper.addRandomEnchantment(new Random(),
						((entity instanceof LivingEntity) ? ((LivingEntity) entity).getItemStackFromSlot(EquipmentSlotType.HEAD) : ItemStack.EMPTY),
						(int) ((entity instanceof PlayerEntity) ? ((PlayerEntity) entity).experienceLevel : 0), (false))));
			if (entity instanceof ServerPlayerEntity)
				((ServerPlayerEntity) entity).inventory.markDirty();
		}
		if (entity instanceof PlayerEntity)
			((PlayerEntity) entity).addExperienceLevel(-((int) ((entity instanceof PlayerEntity) ? ((PlayerEntity) entity).experienceLevel : 0)));
	}
}
